/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 * iSulad licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 * Author: jikai
 * Create: 2024-03-25
 * Description: provide common event definition
 ******************************************************************************/

#ifndef DAEMON_MAILBOX_MAILBOX_H
#define DAEMON_MAILBOX_MAILBOX_H

#include "daemon_arguments.h"
#include "blocking_queue.h"
#include "message_queue.h"
#include "message_subscriber.h"

#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
    MAILBOX_TOPIC_INVALID = -1,
    MAILBOX_TOPIC_CRI_CONTAINER,
    MAILBOX_TOPIC_MAX
} mailbox_topic;

// for async message, it generates a true message to publish
// for sync message, it is the callback function to handle the data to publish
typedef void *(*message_generator_t)(void *, void *);
// release function of message generated by generator, if any
typedef void (*message_release_t)(void *);

typedef struct {
    // to generate a message
    message_generator_t generator;
    // context of handler
    void *context;
    // release function of message, if any
    message_release_t release;
    // message queue
    message_queue *queue;
    // if registered
    bool registered;
} mailbox_topic_handler_t;

typedef enum {
    CRI_CONTAINER_MESSAGE_TYPE_INVALID = -1,
    CRI_CONTAINER_MESSAGE_TYPE_CREATED,
    CRI_CONTAINER_MESSAGE_TYPE_STARTED,
    CRI_CONTAINER_MESSAGE_TYPE_STOPPED,
    CRI_CONTAINER_MESSAGE_TYPE_DELETED,
    CRI_CONTAINER_MESSAGE_TYPE_MAX
} cri_container_message_type;

typedef struct {
    const char *container_id;
    const char *sandbox_id;
    cri_container_message_type type;
} cri_container_message_t;

int mailbox_register_topic_handler(mailbox_topic topic, message_generator_t generator, void *context,
                                   message_release_t release, bool async);

void mailbox_unregister_topic_handler(mailbox_topic topic);

void mailbox_publish(mailbox_topic topic, void *data);

message_subscriber *mailbox_subscribe(mailbox_topic topic);

void mailbox_unsubscribe(mailbox_topic topic, message_subscriber *sub);

#ifdef __cplusplus
}
#endif

#endif
